package cn.jbolt.ec.commodity;

import cn.jbolt.base.JBoltBaseService;

import cn.jbolt.common.config.Msg;
import cn.jbolt.common.model.BecRetailerCommodity;
import cn.jbolt.common.model.BecRetailerCommodityOption;
import cn.jbolt.common.model.BecRetailerStockChange;
import cn.jbolt.ec.cloud.QnService;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;

import java.util.List;

/**
 * @author xieguo
 * @description 商品选项业务逻辑层
 */
public class CommodityOptionService extends JBoltBaseService<BecRetailerCommodityOption> {


    @Override
    protected BecRetailerCommodityOption dao() {
        return new BecRetailerCommodityOption().dao();
    }

    /**
     * 通过commodity_id查找商品选项详情
     * @param commodity_id
     * @return
     */
    public List<BecRetailerCommodityOption> commodity_option_info(Integer commodity_id){
        Kv param=Kv.by("commodity_id",commodity_id).set("is_del",0);
        return getCommonList("id,title,thumb,price,special_price,stock,special_stock",param);
    }


    /***************************************
     * 后台方法
     * @author  zddomi
     * ***********************************
     */

    /**
     * 获取一个商品的图文详情(富文本框使用！！！！！后期用详细商品介绍 可以更改)
     * @param CommodityId
     * @return
     */
    public BecRetailerCommodityOption get_option(Integer CommodityId) {
        System.out.println(findFirst(Kv.by("commodity_id", CommodityId)));
        return findFirst(Kv.by("commodity_id",CommodityId));
    }
    /**
     * 如果数据库里该商品详情没有 就自动创建出来
     * @param CommodityId
     * @return
     */
    public BecRetailerCommodityOption with_option(Integer CommodityId) {
        BecRetailerCommodityOption becRetailerCommodityOption=get_option(CommodityId);

        if(becRetailerCommodityOption==null){
            becRetailerCommodityOption=new BecRetailerCommodityOption();
            becRetailerCommodityOption.setCommodityId(CommodityId);
            becRetailerCommodityOption.save();
        }
        return becRetailerCommodityOption;
    }

    /**
     * 保存商品textcontent(富文本框使用！！！！)
     * @param userId
     * @param becRetailerCommodityOption
     * @return
     */
    public Ret save(Integer userId, BecRetailerCommodityOption becRetailerCommodityOption) {
        if(becRetailerCommodityOption==null||isOk(becRetailerCommodityOption.getId())||notOk(becRetailerCommodityOption.getCommodityId())){
            return fail(Msg.PARAM_ERROR);
        }
        if(notOk(becRetailerCommodityOption.getTitle())){
            return fail("请填写图文详情");
        }
//        goodsHtmlContent.setUpdateTime(new Date());
//        goodsHtmlContent.setUpdateUserId(userId);
        boolean success=becRetailerCommodityOption.save();
        return success?success("操作成功"):fail("操作失败");
    }


    /**
     * 更新商品textcontent(富文本框使用)
     * @param userId
     * @param becRetailerCommodityOption
     * @return
     */
    public Ret update_text_content(Integer userId, BecRetailerCommodityOption becRetailerCommodityOption) {
        if(becRetailerCommodityOption==null||notOk(becRetailerCommodityOption.getId())||notOk(becRetailerCommodityOption.getCommodityId())){
            return fail(Msg.PARAM_ERROR);
        }
        if(notOk(becRetailerCommodityOption.getTitle())){
            return fail("请填写图文详情");
        }
        System.out.println(becRetailerCommodityOption.getTitle()+"Title");
        boolean success=becRetailerCommodityOption.update();
        return success?success("操作成功"):fail("操作失败");
    }

    /**
     * 保存商品详情
     * @param becRetailerCommodityOption
     * @return
     */
    public Ret save_commodity_options(BecRetailerCommodityOption becRetailerCommodityOption,String price,String special_price){
        if(becRetailerCommodityOption==null){
            return fail(Msg.PARAM_ERROR);
        }
        if (special_price==null){
            Double double_commodity_price = Double.parseDouble(price)*100;
            Integer commodity_price=double_commodity_price.intValue() ;
            becRetailerCommodityOption.setPrice(commodity_price);
        }
        if (special_price!=null){
            Double double_commodity_price = Double.parseDouble(price)*100;
            Integer commodity_price=double_commodity_price.intValue() ;
            becRetailerCommodityOption.setPrice(commodity_price);

            Double double_commodity_price1 = Double.parseDouble(special_price)*100;
            Integer commodity_price1=double_commodity_price1.intValue() ;
            becRetailerCommodityOption.setSpecialPrice(commodity_price1);
        }

        Boolean success= becRetailerCommodityOption.save();
        if (success)
        {
            return ret(success);
        }
        return FAIL;

    }

    /**
     * 更新商品详情
     * @param becRetailerCommodityOption
     * @return
     */

    public Ret update_option(Integer useId,BecRetailerCommodityOption becRetailerCommodityOption,String price,String special_price){
        if(becRetailerCommodityOption==null||notOk(becRetailerCommodityOption.getId())){
            return fail(Msg.PARAM_ERROR);
        }
        if (special_price==null){
            Double double_commodity_price = Double.parseDouble(price)*100;
            Integer commodity_price=double_commodity_price.intValue() ;
            becRetailerCommodityOption.setPrice(commodity_price);
        }
        if (special_price!=null){
            Double double_commodity_price = Double.parseDouble(price)*100;
            Integer commodity_price=double_commodity_price.intValue() ;
            becRetailerCommodityOption.setPrice(commodity_price);

            Double double_commodity_price1 = Double.parseDouble(special_price)*100;
            Integer commodity_price1=double_commodity_price1.intValue() ;
            becRetailerCommodityOption.setSpecialPrice(commodity_price1);
        }



        Integer changestock=0;
        BecRetailerCommodityOption b0 = new BecRetailerCommodityOption();
        BecRetailerCommodityOption b1  = b0.dao().findById(becRetailerCommodityOption.getId());
        BecRetailerStockChange becRetailerStockChange = new BecRetailerStockChange();


        if (becRetailerCommodityOption.getSpecialStock()!=null){
            if (b1.getSpecialStock()==null) b1.setSpecialStock(0);
            changestock=becRetailerCommodityOption.getSpecialStock()-b1.getSpecialStock();
            becRetailerStockChange.setAmount(changestock);
            if (changestock>0){
                becRetailerStockChange.setChangeType(4);
            }
            if (changestock<0){
                becRetailerStockChange.setChangeType(5);
            }
        }

        if (becRetailerCommodityOption.getStock()!=null){
            if (b1.getStock()==null)b1.setStock(0);
            changestock=becRetailerCommodityOption.getStock()-b1.getStock();
            becRetailerStockChange.setAmount(changestock);
            if (changestock>0){
                becRetailerStockChange.setChangeType(4);
            }
            if (changestock<0){
                becRetailerStockChange.setChangeType(5);
            }
        }

        becRetailerStockChange.setChangeTime(System.currentTimeMillis());
        becRetailerStockChange.setCommodityId(becRetailerCommodityOption.getCommodityId());

        becRetailerStockChange.setIsDel(false);
        becRetailerStockChange.setOperator(useId);
        if (changestock!=0){
            becRetailerStockChange.save();
        }
        Boolean  success =becRetailerCommodityOption.update();
        if (success){
            return Ret.ok("msg","更新成功");
        }
        else {
            return fail("更新失败");
        }
    }

    /**
     * 软删除商品详情
     * @param id
     * @return
     */
    public Ret delete_option(Integer id){
        if (id!=null){
            Kv kv = Kv.by("ID",id);
            kv.setIfNotNull("table",table());

            Db.template("ec.mall.manage.commodity.update_option",kv).update();
            return success("删除成功");
        }
        return fail("操作失败");
    }


    /**
     * 更新商品图片
     * @param becRetailerCommodityOption
     * @return
     */
    public Ret update_option_images(BecRetailerCommodityOption becRetailerCommodityOption){
        if(becRetailerCommodityOption==null||notOk(becRetailerCommodityOption.getId())){
            return fail(Msg.PARAM_ERROR);
        }
        if (isOk(becRetailerCommodityOption.getId())){
            boolean success =  becRetailerCommodityOption.update();
            if (success)
                return success("操作成功");
            return fail("操作失败");

        }

        return fail("操作失败");


    }

    /**
     *  秒杀商品 添加时  三级选项数据源
     *  选择商品规格
     * @param commodity_id
     * @return
     */
    public List<BecRetailerCommodityOption> rush_commodity_select_option(Integer commodity_id){
        return getCommonList("id,title",Kv.by("commodity_id",commodity_id));
    }




}
